package com.conorroche.taskmanagement;

//import com.google.android.gms.common.GooglePlayServicesUtil;

import android.os.Bundle;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.database.Cursor;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;

public class TagsActivity extends Activity implements ActionBar.TabListener{

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_tags);

		// Set up the action bar to show tabs.
		actionBar = getActionBar();
		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

		// For each of the sections in the app, add a tab to the action bar.
		actionBar.addTab(actionBar.newTab().setText(R.string.title_activity_tasks)
				.setTabListener(this));
		actionBar.addTab(actionBar.newTab().setText(R.string.title_activity_tags)
				.setTabListener(this));
		actionBar.addTab(actionBar.newTab().setText(R.string.title_activity_location)
				.setTabListener(this));
		actionBar.setSelectedNavigationItem(1);
		lst = (ListView) findViewById(R.id.TagsListView);
		tagsHelper = new TagsHelper(getApplicationContext(), lst);
	
		tagsHelper.getTagView();
		tagsList = tagsHelper.getList();
		tagsList.setOnItemClickListener(new OnItemClickListener(){
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				String[] tags = tagsHelper.getTags();
				DBAdapter dba = new DBAdapter(getApplicationContext());
				dba.open();
				Cursor taggedTasks = dba.getTaggedTasks(tags[arg2]);
				if(taggedTasks.getCount() > 0){
					Intent tagsIntent = new Intent(getApplicationContext(), TaggedTasksActivity.class);
					tagsIntent.putExtra("clicked", tags[arg2]);
					startActivity(tagsIntent);
				}else{
					Toast.makeText(getApplicationContext(), "There are no tasks with that tag.\nWhy not create some!", Toast.LENGTH_SHORT).show();
				}
				dba.close();
			}
		});
	}
	protected void onResume(){
		super.onResume();
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.tags, menu);
		return true;
	}
	@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        	case R.id.add_task:
        		addTask = new Intent(getApplicationContext(), AddTask.class);
            	addTask.putExtra("edit", false);
        		startActivity(addTask);
            	return true;
        	case R.id.add_tag:
        		addTag = new Intent(getApplicationContext(), AddTag.class);
        		startActivity(addTag);
        		return true;
        	case R.id.menu_legalnotices:
//        		String LicenseInfo = GooglePlayServicesUtil.getOpenSourceSoftwareLicenseInfo(getApplicationContext());
        	    AlertDialog.Builder LicenseDialog = new AlertDialog.Builder(TagsActivity.this);
        	    LicenseDialog.setTitle("Legal Notices");
//        	    LicenseDialog.setMessage(LicenseInfo);
        	    LicenseDialog.show();
        	    return true;
        }
        return super.onOptionsItemSelected(item);
        
	}
	@Override
	public void onTabReselected(Tab tab, FragmentTransaction ft) {
		
	}

	@Override
	public void onTabSelected(Tab tab, FragmentTransaction ft) {
		switch(tab.getPosition()){
			case 0:	// all tasks
				if(first){
					first = false;
				}else{
					Intent allTasks = new Intent(getApplicationContext(), MainActivity.class);
					startActivity(allTasks);
				}
			break;
			case 1: //all tags
			break;
			case 2:
				//do something
				Intent map = new Intent(getApplicationContext(), LocationActivity.class);
				startActivity(map);
			break;
		}
		
	}

	@Override
	public void onTabUnselected(Tab tab, FragmentTransaction ft) {
		
	}
	
	private Intent addTask, addTag;
	private ListView lst;
	private static TagsHelper tagsHelper;
	private static ListView tagsList;
	private boolean first = true;
	public ActionBar actionBar;
}
